package aspectos;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import observable.ObservableObject;
import observable.ObservableValidadores;
import uqbar.aspectj.utils.Utils;
import dominio_b.Validable_b;

public aspect ValidadorAspectMixin {

	declare parents : @Validable_b * implements ObservableValidadores;

	public pointcut verNombre(ObservableValidadores target, Object value) : set(* (@Validable_b  *..*).*)  
	    && args(value) 
		&& target( @Validable_b target)
		&& within( @Validable_b *);

	
	void around(ObservableValidadores target, Object value)    : verNombre( target,value) {

		String fieldName = thisJoinPoint.getSignature().getName();
		Object oldValue = Utils.getField(target, fieldName);
		target.fieldChanged(fieldName, oldValue, value);

		proceed(target, value);
		if (oldValue != value) {
			target.fieldChanged(fieldName, oldValue, value);
		}

	}

	after(ObservableValidadores target) : initialization(* ..*.new(..) ) && target(target) {
		target.initialize();
	}

	private transient PropertyChangeSupport ObservableObject.changeSupport;

	public void ObservableObject.initialize() {
		this.changeSupport = new PropertyChangeSupport(this);
	}

	public void ObservableObject.agregarValidador(String propertyName, PropertyChangeListener listener) {
		this.changeSupport.addPropertyChangeListener(propertyName, listener);
	}
	
	public void ObservableObject.removerValidador(String propertyName, PropertyChangeListener listener) {
		this.changeSupport.removePropertyChangeListener(propertyName, listener);
	}

	public void ObservableObject.fieldChanged(String fieldName,
			Object oldValue, Object newValue) {
		this.changeSupport.firePropertyChange(fieldName, oldValue, newValue);
	}

}
